/*
	Looks at distribution of college graduate quality by country.
	Output:	Figure 4a
			Figure 4b
			Figure 4c
*/
 
local seed "C:\Users\jsock\Dropbox\Research\GD\International"

local dataPath "`seed'/Data"
local inputPath "`seed'/InputData"
local figurePath "`seed'/Replication/Figures"
local tablePath "`seed'/Replication/Tables"
local estimatePath "`seed'/Replication/Estimates"
local tempPath "`seed'/Replication/TempData"

clear 
set more off
set scheme s1mono

insheet using "`estimatePath'/School_q_j_distribution_relative_US.csv" , comma
drop v1 
 
*--------------------------------- 
* Generate cumulative distribution by country of distribution relative US
*---------------------------------
 
bys university_country_iso: cumul fit_ecdf_usa, gen(ecdf_q_j)

sort university_country_iso fit_ecdf_usa ecdf_q_j

*---------------------------------
* Generate comparison with U.S. quantiles
*---------------------------------

summarize q_j if university_country_iso == "USA" , detail
scalar us_q_0 = r(min)
scalar us_q_25 = r(p25)
scalar us_q_50 = r(p50)
scalar us_q_75 = r(p75)
scalar us_q_100 = r(max)

* Four bins
generate us_bin = .
replace us_bin = 1 if q_j < us_q_25
replace us_bin = 2 if q_j >= us_q_25 & q_j < us_q_50
replace us_bin = 3 if q_j >= us_q_50 & q_j < us_q_75
replace us_bin = 4 if q_j >= us_q_75 

bys university_country_iso: gen schools = _N
bys university_country_iso us_bin : gen binschools = _N

generate share_us_bin = binschools / schools

bys university_country_iso us_bin : keep if _n == 1

gen new_id = _n
bys university_country_iso : replace new_id = . if _n > 1
bys university_country_iso : egen id = max(new_id)

xtset id us_bin 
tsfill , full

sort id university_country_iso
by id : gen country = university_country_iso[_N]
replace share_us_bin = 0 if share_us_bin == .

sort id us_bin

*---------------------------------
* Generate different var for each country
*---------------------------------

local nations "BRA CAN CHN IND GBR NGA PHL PAK MYS USA EGY IRL MEX BGD LKA ZAF GRC SGP"

foreach nation of local nations{

	generate share_us_bin_`nation' = share_us_bin if country == "`nation'"

}

collapse (mean) share_us_bin_*, by(us_bin)

* Wealthy Countries 

graph bar share_us_bin_CAN share_us_bin_IRL share_us_bin_GBR share_us_bin_USA , over(us_bin, relabel(1 "US-Q1" 2 "US-Q2" 3 "US-Q3" 4 "US-Q4")) yscale(range(0(0.25)1)) ylabel(0(0.25)1) scale(1.1) ytitle("share of colleges") ///
legend(label(1 "Canada") label(2 "Ireland") label(3 "United Kingdom")  label(4 "United States")) bar(1, color(gs13)) bar(2, color(gs9))  bar(3, color(gs5))  bar(4, color(gs1)) ///
legend(position(0) bplacement(neast) row(4)) 
graph export "`figurePath'/Figure_4a.eps" , replace

* China and India

graph bar share_us_bin_CHN share_us_bin_IND , over(us_bin, relabel(1 "US-Q1" 2 "US-Q2" 3 "US-Q3" 4 "US-Q4")) yscale(range(0(0.25)1)) ylabel(0(0.25)1) scale(1.1) ytitle("share of colleges")  ///
legend(label(1 "China") label(2 "India"))  bar(1, color(gs10))  bar(2, color(gs3)) ///
legend(position(0) bplacement(neast) row(2)) 
graph export "`figurePath'/Figure_4b.eps" , replace

* Other Poor Countries

graph bar share_us_bin_BRA share_us_bin_EGY share_us_bin_NGA share_us_bin_ZAF share_us_bin_PAK share_us_bin_PHL share_us_bin_MYS , over(us_bin, relabel(1 "US-Q1" 2 "US-Q2" 3 "US-Q3" 4 "US-Q4")) yscale(range(0(0.25)1)) ylabel(0(0.25)1) scale(1.1) ytitle("share of colleges") ///
legend(label(1 "Brazil") label(2 "Egypt") label(3 "Nigeria")  label(4 "South Africa") label(5 "Pakistan")  label(6 "Philippines")  label(7 "Malaysia")) bar(1, color(gs14)) bar(2, color(gs12)) bar(3, color(gs10) bstyle(gs8)) bar(4, color(gs6)) bar(5, color(gs4)) bar(6, color(gs2)) bar(7, color(gs0))  ///
legend(position(0) bplacement(neast) row(7)) 
graph export "`figurePath'/Figure_4c.eps" , replace



